<template>
  <div class="search-top">
    <div class="left">
      <svg class="icon" @click="$router.go(-1)" aria-hidden="true">
        <use xlink:href="#icon-zuojiantou" />
      </svg>
      <div class="top-input">
        <input type="text" v-model="searchKey" :placeholder="placeholder" @keyup.enter="saveKW" />
      </div>
    </div>
    <div class="history" v-show="musicList.length==0">
      <div class="history-left">历史</div>
      <div class="history-right">
        <div
          class="historyitem"
          v-for="(item,i) in keywordList"
          :key="i"
          @click="historySearch(item)"
        >{{item}}</div>
      </div>
    </div>

    <div class="musicList" v-show="musicList.length!==0">
      <div class="song-content">
        <div class="song" v-for="(item,i) in musicList" :key="item.id" @click="setPlay(item,i)">
          <div class="song-left">
            <div class="song-information">
              <div class="song-title">{{item.name}}</div>
              <div class="singer" v-for="singer in item.artists" :key="singer.id">
                <span class="singerName">{{singer.name}}/</span>
              </div>
            </div>
          </div>
          <div class="song-right">
            <svg class="icon play" aria-hidden="true">
              <use xlink:href="#icon-bofang1" />
            </svg>
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icon-gengduo" />
            </svg>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { searchMusic, defaultSearch } from "@/api/index.js";
export default {
  data() {
    return {
      placeholder: "",
      keywordList: [],
      searchKey: "",
      musicList: []
    };
  },
  methods: {
    async saveKW() {
      this.keywordList.push(this.searchKey);
      this.keywordList.push(this.placeholder);
      this.keywordList = Array.from(new Set(this.keywordList));
      if (this.keywordList.length > 10) {
        this.keywordList = this.keywordList.slice(
          this.keywordList.length - 10,
          this.keywordList.length
        );
      }
      localStorage.keywordList = JSON.stringify(this.keywordList);
      let result = await searchMusic(this.searchKey);
      this.musicList = result.data.result.songs;
      console.log(this.musicList);
    },

    historySearch(keyword) {
      this.searchKey = keyword;
      this.saveKW();
    },
    setPlay(item, i) {
      item.al = item.album;
      console.log(item);
      item.al.picUrl = item.album.artist.img1v1Url;
      this.$store.commit("pushPlayList", item);
      this.$store.commit("setPlay", this.$store.state.playList.length - 1);
    }
  },
  beforeMount() {
    console.log(111);
    this.keywordList = localStorage.keywordList
      ? JSON.parse(localStorage.keywordList)
      : [];
  },
  async mounted() {
    let res = await defaultSearch();
    console.log(res);
    this.placeholder = res.data.data.showKeyword;
  }
};
</script>
<style scoped lang='less'>
.search-top {
  height: 1rem;
  width: 7.5rem;
  .left {
    width: 100%;
    padding: 0 0.4rem;
    display: flex;
    align-items: center;
    .icon {
      width: 0.4rem;
      height: 0.4rem;
      fill: black;
    }
    .top-input {
      padding: 0 0 0 0.4rem;
      flex: 1;
      input {
        width: 100%;
        border: none;
        outline: none;
        border-bottom: 2px solid #ccc;
        height: 0.8rem;
        font-size: 0.24rem;
        font-family: "微软雅黑";
      }
    }
  }
  .history {
    display: flex;

    .history-left {
      width: 0.6rem;
      height: 0.6rem;
      font-weight: 600;
      margin: 0 0.2rem;
      line-height: 0.6rem;
    }
    .history-right {
      color: #666;
      display: flex;
      flex-wrap: wrap;
      flex: 1;
      .historyitem {
        background-color: #f3f3f3;
        padding: 0 0.2rem;
        margin: 0.05rem 0.1rem;
        height: 0.6rem;
        line-height: 0.6rem;
        border: 1px solid #ccc;
        border-radius: 0.3rem;
        font-size: 0.26rem;
      }
    }
  }
  .musicList {
    height: calc(100vh - 2rem);
    overflow: scroll;
    .song-content {
      .song {
        margin-top: 0.2rem;
        display: flex;
        justify-content: space-between;
        .song-left {
          display: flex;
          align-items: center;

          .song-information {
            margin: 0 0.4rem;
            .song-title {
              font-size: 0.28rem;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 1;
              -webkit-box-orient: vertical;
            }
            .singer {
              display: flex;
              float: left;
              overflow: hidden;
              text-overflow: ellipsis;
              display: -webkit-box;
              -webkit-line-clamp: 1;
              -webkit-box-orient: vertical;
              .singerName {
                font-family: "微软雅黑";
                font-size: 0.22rem;
                color: #c2c2c2;
              }
            }
          }
        }
        .song-right {
          display: flex;
          align-items: center;
          .icon {
            width: 0.4rem;
            height: 0.4rem;
            margin-right: 0.3rem;
            fill: #aaa;
          }
        }
      }
    }
  }
}
</style>